package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.gxwzxy.medic.entity.Role;
import com.gxwzxy.medic.service.MenuService;
import com.gxwzxy.medic.service.RoleService;
import com.gxwzxy.medic.service.Impl.MenuServiceImpl;
import com.gxwzxy.medic.service.Impl.RoleServiceImpl;
import com.my.web.servlet.RequestBeanUtils;


/**
 * Servlet implementation class RoleServlect
 */
public class RoleServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 椤甸潰璇锋眰鏌ヨ瑙掕壊
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findRole(HttpServletRequest request,HttpServletResponse response) throws IOException ,ServletException{
		//鑾峰彇鍙傛暟
		String fr = request.getParameter("fr");	
		//查询出全部的role
		RoleService rs = new RoleServiceImpl();
		List<Role> lr = rs.findRole();
		request.setAttribute("lr", lr);
		//鏍规嵁fr鍒ゆ柇杩斿洖椤甸潰	
		if(fr.equals("addUser")) {
			//杩斿洖娣诲姞鐢ㄦ埛鐣岄潰
			request.getRequestDispatcher("page/user/addUser.jsp").forward(request, response);
		}else if(fr.equals("updateUser")) {
			//淇敼鐢ㄦ埛鏃跺紓姝ヤ紶鍊�
			response.getWriter().write(new Gson().toJson(lr));
		}else if(fr.equals("role")) {
			//杩斿洖瑙掕壊鐣岄潰
			request.getRequestDispatcher("page/role/role.jsp").forward(request, response);
		}
		
	}
	/**
	 * 根据roleId删除角色
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void RoleDelete(HttpServletRequest request,HttpServletResponse response) throws IOException ,ServletException{
		//鑾峰彇鍝嶅簲鏁版嵁
		Role role = RequestBeanUtils.requestToBean(request, Role.class);
		//澶勭悊璇锋眰淇℃伅
		RoleService rs = new RoleServiceImpl();
		boolean bl = rs.RoleDelete(role.getRoleId());
		if(bl) {
			response.getWriter().write("true");
		}else {
			response.getWriter().write("false");
		}
	}
	
	
	/**
	 * 娣诲姞瑙掕壊
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		//鑾峰彇鍝嶅簲鏁版嵁
		Role role = RequestBeanUtils.requestToBean(request, Role.class);
		String [] menuId = request.getParameterValues("menuId");
		System.out.println(menuId+" "+role.getRoleId()+" "+role.getRoleName());
		RoleService rs = new RoleServiceImpl();
		boolean bl = rs.addRole(role,menuId);
		if(bl) {
			request.setAttribute("url", "RoleServlet?method=findRole&&fr=role");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
   }
	
	/**
	 * 鏍规嵁roleId淇敼瑙掕壊淇℃伅
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		//鑾峰彇鍝嶅簲鏁版嵁
		Role role = RequestBeanUtils.requestToBean(request, Role.class);
		String []menuId = request.getParameterValues("menuId");
		//澶勭悊璇锋眰淇℃伅
		//淇敼瑙掕壊琛�
		RoleService rs = new RoleServiceImpl();
		boolean bl = rs.updateRoleByroleId(role.getRoleId(),role.getRoleName());
		//淇敼瑙掕壊鏉冮檺琛�
		MenuService ms = new MenuServiceImpl();
		boolean bn = ms.updateMenuByroleId(role.getRoleId(),menuId);
		if(bl) {
			if(bn) {
				request.setAttribute("url", "RoleServlet?method=findRole&&fr=role");
				request.getRequestDispatcher("tips.jsp").forward(request, response);
			}
		}
	}
	
//	public void Test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//		System.out.println("杩涘叆鏂规硶");
//		String [] id = request.getParameterValues("id");
//		System.out.println("id鐨勯暱搴︼細"+id.length);
//		String [] name = request.getParameterValues("name");
//		String [] pid = request.getParameterValues("pid");
//		for(int i = 0;i<id.length;i++) {
//			System.out.println(id[i]+" ");
//			System.out.println(name[i]+" ");
//			System.out.println(pid[i]);
//		}
//	}
	
}
